package sg.edu.nus.iss.cats.util;

/** coverts String to java.util.Date using BeanUtils ***/

import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.converters.*;
import java.text.*;
import java.util.*;

public class DateToStringBeanUtilsConverter implements Converter {
    private static final StringConverter stringConverter = new StringConverter();
    private String formatPattern = null;

    public void setFormatPattern(String formatPattern) {
        this.formatPattern = formatPattern;
    }

    public Object convert (Class type, Object value) {
        Object returnValue = null;
        if (value != null) {
            if (type == String.class && (value instanceof Date)) {
                SimpleDateFormat formatter =
                    new SimpleDateFormat (formatPattern);
                String dateString = formatter.format (value);
                returnValue = dateString;
            } else {
                returnValue = stringConverter.convert (type, value);
            }
        }
        return returnValue;
    }
}
